
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" >
        <title>支付</title>
        <link rel="stylesheet" href="../vender/css/normalize.css">
        <link rel="stylesheet" href="pay-page.css">
        <link rel="stylesheet" href="../../public_css/public_header.css">
    </head>
    <body>
        <header class=" public_header">
            <div class="public_header_return">
                <img src="../../public_img/return.png" alt="" class="public_header_return" onclick="history.back(-1)">
                <p>支付详情</p>
            </div>
        </header>
        <section>
            <ul>
                <li class="how-much-money">
                    <span>总计:</span>
                    <span class="place">加载中...</span>
                </li>
                <li>
                    <form>
                        <label >
                            <span>支付宝</span>
                            <input type="radio" name="payment_type" class="pay"  value="zfb" checked disabled><i></i>
                        </label>
                        <!--<label >
<span>微信</span>
<input type="radio" name="payment_type" disabeld class="pay" value="wx"><i></i>
</label>-->
                    </form>
                </li>
            </ul>
        </section>
        <footer>
            <button class="btn_comfirm" id="butt" >立即支付</button>
        </footer>
        <input type="hidden" id="place" value="zfb">
        <input type="hidden" id="token" value="<?php echo $_GET['token'] ?>">
        <input type="hidden" id="type" value="<?php echo $_GET['sign_type'] ?>">
        <input type="hidden" id="id" value="<?php echo $_GET['school_id'] ?>">
        <script src="jquery.min.js"></script>
        <script src="../vender/src/picturefill.js"></script>
        <script src="index.js"></script>
        <script src="../../layer.js"></script>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">


            var place =localStorage.place;
            /*console.log(place)*/
            $('.place').html(place+'元');
            var token =localStorage.phone;
            var sign_type =localStorage.sign_type;
            var school_id =localStorage.school_id;
            var sign_place=place;
            var payment_type='';
            var ALIPAYSERVER='';
            function goback(){
                history.back();
            }
            var wxChannel = null; // 微信支付
            var aliChannel = null; // 支付宝支付
            var channel = null;
            mui.init({
                swipeBack:true //启用右滑关闭功能
            });

            mui.plusReady(function() {
                // 获取支付通道
                plus.payment.getChannels(function(channels){
                    aliChannel=channels[0];
                    wxChannel=channels[1];
                },function(e){
                    alert("获取支付通道失败："+e.message);
                });
            })


            document.getElementById('butt').addEventListener('tap',function() {



                $("input[name='payment_type']").each(function () {
                    if($(this).prop('checked')){
                        payment_type=$(this).val();
                    }
                });
                if(payment_type==''){
                    layer.msg("请选择支付方式");
                }else{
                    $.ajax({
                        url:"https://newapp.icandrive.cn/api/sign.php",
                        data:{token:token,
                              user_name: localStorage.examination_name,
                              user_phone:localStorage.examination_no_phone,  sign_type:sign_type,
                              sign_place:sign_place,
                              payment_type:payment_type,
                              sign_type:localStorage.sign_type,
                              school_id:localStorage.payschool_id
                             },
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            console.log(data);
                            if(data.status==200){
                                ALIPAYSERVER='https://newapp.icandrive.cn/api/alipay.php?total='+sign_place+"&order_number="+data.order_number;
                                pay('alipay');

                            }else{
                                layer.msg("保存订单失败");
                            }
                        },
                        error:function(){
                            layer.msg('系统维护中',function(){
                                history.go(0)
                            },1000);
                        }
                    });
                    $(this).unbind();
                }

            })


            // 2. 发起支付请求
            function pay(id){
                console.log(ALIPAYSERVER)

                PAYSERVER=ALIPAYSERVER;
                channel = aliChannel;
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    switch(xhr.readyState){
                        case 4:
                            if(xhr.status==200){
                                plus.payment.request(channel,xhr.responseText,function(result){
                                    plus.nativeUI.alert("支付成功！",function(){
                                        $.ajax({
                                            url:"https://newapp.icandrive.cn/api/addScore.php",
                                            data:{token:token,

                                                  type:'报名'
                                                 },
                                            dataType:'json',
                                            type:'post',
                                            success:function(data){

                                                if(data.status==200){
                                                   history.back();

                                                }else{
                                                    layer.msg("添加积分失败");
                                                }
                                            },
                                            error:function(){
                                                layer.msg('系统维护中',function(){
                                                    history.go(0)
                                                },1000);
                                            }
                                        });


                                    });
                                },function(error){
                                    plus.nativeUI.alert("支付失败：" + error.code);
                                });
                            }else{
                                alert("获取订单信息失败！");
                            }
                            break;
                        default:
                            break;
                                         }
                }
                xhr.open('GET',PAYSERVER);
                xhr.send();
            }
        </script>
    </body>
</html>
